package com.bum.api.core.bo;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;

import com.bum.api.core.exp.om.Metas;
import com.bum.api.core.exp.om.Params;

public class WelcomeBO {
	boolean		debugMode	= false;
	boolean		checkAgent	= true;

	ClientBO	clientBO;

	/**
	 * OLA WORKING SESSION
	 */
	public void welcome(String path, Params params, Metas metas) throws Exception {
		/**
		 * Check đủ fields cần thiết không
		 */
//		String owsc = params.getValue("owsc");
//		if (StringUtils.isBlank(owsc)) {
//			if (owsc == null || !isGuestPath(path, metas)) {
//				throw new Exception("Invalid signature");
//			}
//		}

		String cid = params.getValue("cid");
		String did = params.getValue("did");
//		if (StringUtils.isBlank(cid)) {
//			throw new Exception("Invalid signature");
//		}

//		String sig = params.getValue("sig");
//		if (StringUtils.isBlank(sig)) {
//			throw new Exception("Invalid signature");
//		}

		/**
		 * Check clientID
		 */
//		if (checkAgent) {
//			clientBO.checkClientAgent(cid, metas.get(Metas.USER_AGENT), debugMode, params);
//		}
		metas.put("cid", cid);
		metas.put("did", did);

		/**
		 * Checksum
		 */
//		String signature = null;
//		if (sig.length() == 32) {
//			signature = genSignature(path, params);
//		} else {
//			// chữ ký kiểu mới, đơn giản
//			signature = genSignatureSimple(path, params);
//		}
//		if (!debugMode && !signature.equals(sig)) {
//			throw new Exception("Invalid signature");
//		}

		String apv = params.getValue("apv");
		if (StringUtils.isNotBlank(apv)) {
			metas.put(Metas.APK_VERSION, apv);
		}

		if (debugMode) {
			metas.put(Metas.DEBUG_MODE, "1");
		}
	}

	boolean isGuestPath(String path, Metas metas) {
		return "1".equals(metas.get("isGuestPath"));
	}

	String genSignature(String path, Params params) {
		String seeds = genSeeds(path, params);
		String signature = DigestUtils.md5Hex(seeds);
		return signature;
	}

	private String genSeeds(String path, Params params) {
		List<String> items = new LinkedList<String>();
		items.add(path);

		for (String key : params.keySet()) {
			if ("sig".equals(key)) {
				continue;
			}
			for (String val : params.getValues(key)) {
				items.add(key + "=" + val);
			}
		}
		Collections.sort(items);

		String seeds = StringUtils.join(items, " ");
		return seeds;
	}

	String genSignatureSimple(String path, Params params) {
		String seeds = genSeeds(path, params);
		int hash = Math.abs(seeds.hashCode());
		String signature = String.valueOf(hash);
		return signature;
	}

	/**
	 * SETTERS
	 */
	public void setDebugMode(boolean debugMode) {
		this.debugMode = debugMode;
	}

	public void setClientBO(ClientBO clientBO) {
		this.clientBO = clientBO;
	}

	public void setCheckAgent(boolean checkAgent) {
		this.checkAgent = checkAgent;
	}
}
